#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=..\Resources\Icons\Install-Green.ico
#AutoIt3Wrapper_Outfile=Distribute\Get-App.exe
#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
$GetAppVersion = "0.2.0 Alpha"
$reqDays = 1
$server = ""
$iniFile = @ScriptDir & "\Get-App.conf"
$dataPort = "277"
$filePort = "438"
$latest = True
$Name = ""
$Version = ""
$uri = ""
$uninstall = False
$reinstall = False
$fast = True
$fast = False
$keepFile = False
Global $apiType = "Undefined"
SplashScreen()
ConsoleWrite(" Starting..." & @CRLF)
If $fast == False Then Sleep("2000")
checkINI()
RequiredApps()
grabParams()
GetAppInfo("")
If $apiType == "group" Then
	InstallGroup()
ElseIf $apiType == "app" Then
	If $uninstall == True Then
		UninstallFile()
	ElseIf $reinstall == True Then
		ReInstallFile()
	Else
		InstallFile()
	EndIf
EndIf
UpdateGetApp()
ConsoleWrite(" " & @CRLF)
ConsoleWrite(" ...Done!" & @CRLF)
ConsoleWrite(" " & @CRLF)
ConsoleWrite(" " & @CRLF)
ConsoleWrite(" " & @CRLF)
If $fast == False Then Sleep("5000")
Func checkINI()
	if FileExists("C:\Windows\" & $iniFile) Then
		$server = IniRead("C:\Windows\" & $iniFile,"Config","Server","http://Get-App")
		$dataPort = IniRead("C:\Windows\" & $iniFile,"Config","Data Port","277")
		$filePort = IniRead("C:\Windows\" & $iniFile,"Config","File Port","438")
	ElseIf FileExists($iniFile) Then
		$server = IniRead($iniFile,"Config","Server","http://Get-App")
		$dataPort = IniRead($iniFile,"Config","Data Port","277")
		$filePort = IniRead($iniFile,"Config","File Port","438")
	ElseIf FileExists(@ScriptDir & "\" & $iniFile) Then
		$server = IniRead(@ScriptDir & "\" & $iniFile,"Config","Server","http://Get-App")
		$dataPort = IniRead(@ScriptDir & "\" & $iniFile,"Config","Data Port","277")
		$filePort = IniRead(@ScriptDir & "\" & $iniFile,"Config","File Port","438")
	EndIf
	ConsoleWrite(" " & @CRLF)
	ConsoleWrite("     [*] Server is set to: " & @TAB & StringTrimLeft($server,7) & @CRLF)
	ConsoleWrite("     [*] Data port set to: " & @TAB & $dataPort & @CRLF)
	ConsoleWrite("     [*] File port set to: " & @TAB & $filePort & @CRLF)
EndFunc
Func grabParams()
	If $CMDLINE[0] >= 1 Then
		Local $i
		For $i = 1 to $CMDLINE[0]
			If $CMDLINE[$i] == "-s" Or $CMDLINE[$i] == "--server" Then
				$server = "http://" & $CMDLINE[$i + 1]
			ElseIf $CMDLINE[$i] == "-u" Or $CMDLINE[$i] == "--un-install" Or $CMDLINE[$i] == "--uninstall" Then
				$apiType = "app"
				$Name = $CMDLINE[$i + 1]
				$uninstall = True
			ElseIf $CMDLINE[$i] == "-r" Or $CMDLINE[$i] == "--reinstall" Or $CMDLINE[$i] == "--re-install" Then
				$apiType = "app"
				$Name = $CMDLINE[$i + 1]
				$reinstall = True
			ElseIf $CMDLINE[$i] == "--update" Or $CMDLINE[$i] == "-update" Then
				$apiType = "app"
				$Name = "getapp"
			ElseIf $CMDLINE[$i] == "--keep-file" Or $CMDLINE[$i] == "-kf" Then
				$keepFile = True
			ElseIf $CMDLINE[$i] == "-dp" Or $CMDLINE[$i] == "--data-port" Then
				$dataPort = $CMDLINE[$i + 1]
			ElseIf $CMDLINE[$i] == "-fp" Or $CMDLINE[$i] == "--file-port" Then
				$filePort = $CMDLINE[$i + 1]
			ElseIf $CMDLINE[$i] == "-g" Or $CMDLINE[$i] == "--group" Or $CMDLINE[$i] == "-group" Then
				$apiType = "group"
				$Name = $CMDLINE[$i + 1]
			ElseIf $CMDLINE[$i] == "-v" Or $CMDLINE[$i] == "--version" Or $CMDLINE[$i] == "-ver" Then
				$latest = False
				$Version = $CMDLINE[$i + 1]
			ElseIf $CMDLINE[$i] == "-a" Or $CMDLINE[$i] == "--app" Or $CMDLINE[$i] == "-app" Then
				$apiType = "app"
				$Name = $CMDLINE[$i + 1]
			ElseIf $apiType == "Undefined" Then
				showHelp()
			EndIf
		Next
	Else
		If $CMDLINE[0] <= 1 Then
			showHelp()
		EndIf
	EndIf
EndFunc
Func GetAppInfo($uri_)
	If $latest == True Then
		$uri = $server & ":" & $dataPort & "/" & $apiType & "/" & $Name
	Else
		$uri = $server & ":" & $dataPort & "/" & $apiType & "/" & $Name & "/" & $Version
	EndIf
	If IsDeclared($uri_) Then
		If $uri_ <> "" Then $uri = $uri_
	EndIf
	;MsgBox(0,"",$uri,1)
	Local $sData = InetRead($uri)
	Local $nBytesRead = @extended
	Global $appInfoCSV = BinaryToString($sData)
	Return BinaryToString($sData)
EndFunc
Func InstallFile()
	If $appInfoCSV == "" Then
		ConsoleWrite("     [!] Could not find " & $CMDLINE[2] & " in the repository " & $server & "." & @CRLF)
		ConsoleWrite("     [!] Using URI: " & $uri & @CRLF)
		If $fast == False Then Sleep("5000")
	Else
		Local $appInfo = StringSplit($appInfoCSV,",")
		InetGet($server & ":" & $filePort & "/" & $appInfo[2] & "/" & $appInfo[1],@ScriptDir & "\" & $appInfo[1])
		ConsoleWrite("     [*] Installing " & $appInfo[2] & " " & $appInfo[1] & @CRLF)
		ConsoleWrite("     [*] URI " & $server & ":" & $filePort & "/" & $appInfo[2] & "/" & $appInfo[1] & @CRLF)
		ConsoleWrite("     [*] Saving Temporarily as " & @ScriptDir & "\" & $appInfo[1] & @CRLF)
		ConsoleWrite("     [*] Running " & @ScriptDir & "\" & $appInfo[1] & " " & $appInfo[3] & @CRLF)
		;MsgBox(0,"",@ScriptDir & "\" & $appInfo[1] & " " & $appInfo[3])
		;RunWait(@ScriptDir & "\" & $appInfo[1] & " " & $appInfo[3])
		ShellExecuteWait(@ScriptDir & "\" & $appInfo[1] , $appInfo[3])
		If $keepFile <> True Then FileDelete(@ScriptDir & "\" & $appInfo[1])
	EndIf
EndFunc
Func UninstallFile()
	ConsoleWrite("     [*] Uninstalling " & $Name & "..." & @CRLF)
	Local $appInfo = StringSplit($appInfoCSV,",")
	If $appInfo[4] <> "None" Then
		Local $unCMDs = StringSplit($appInfo[4],"&")
		For $i = 1 To $unCMDs[0]
			;MsgBox(0,"",$unCMDs[$i],1)
			ConsoleWrite("     [*] Running " & $unCMDs[$i] & "..." & @CRLF)
			RunWait($unCMDs[$i])
		Next
	Else
		ConsoleWrite("     [!] No uninstall specified in database..." & @CRLF)
	EndIf
EndFunc
Func ReInstallFile()
	UninstallFile()
	InstallFile()
EndFunc
Func showHelp()
	ConsoleWrite(" " & @CRLF)
	ConsoleWrite(@TAB & "Get-App" & @CRLF)
	ConsoleWrite("  Josiah Kerley" & @CRLF)
	ConsoleWrite(" " & @CRLF)
	ConsoleWrite(@TAB & "Command Structure:" & @CRLF)
	ConsoleWrite(" Get-App <type> <name> [<-v|--version> <version>]" & @CRLF)
	ConsoleWrite(" " & @CRLF)
	ConsoleWrite(@TAB & "Types:" & @CRLF)
	ConsoleWrite(" -a, --app"&@TAB&@TAB&"-"&@TAB&"  Installs an application by name" & @CRLF)
	ConsoleWrite(" -g, --group"&@TAB&@TAB&"-"&@TAB&"  Installs an application by group" & @CRLF)
	ConsoleWrite(" -v, --version"&@TAB&@TAB&"-"&@TAB&"  Specifies version" & @CRLF)
	ConsoleWrite(" -r, --re-install"&@TAB&"-"&@TAB&"  Reinstalls app" & @CRLF)
	ConsoleWrite(" -u, --un-install"&@TAB&"-"&@TAB&"  Uninstalls app" & @CRLF)
	ConsoleWrite(" --update"&@TAB&@TAB&"-"&@TAB&"  Updates Get-App client to latest version" & @CRLF)
	ConsoleWrite(" " & @CRLF)
	ConsoleWrite(@TAB & "Example:" & @CRLF)
	ConsoleWrite(" app-get -a flash"&@TAB&"-"&@TAB&" Installs application flash from server." & @CRLF)
	ConsoleWrite(" app-get -g creative"&@TAB&"-"&@TAB&" Installs applications under the group creative from server." & @CRLF)
	ConsoleWrite(" " & @CRLF)
	exit
EndFunc
Func SplashScreen()
	ConsoleWrite(" " & @CRLF)
	ConsoleWrite(" " & @CRLF)
	ConsoleWrite(" " & @CRLF)
	ConsoleWrite("   /$$$$$$              /$$           /$$$$$$                     " & @CRLF)
	ConsoleWrite("  /$$__  $$            | $$          /$$__  $$                    " & @CRLF)
	ConsoleWrite(" | $$  \__/  /$$$$$$  /$$$$$$       | $$  \ $$  /$$$$$$   /$$$$$$ " & @CRLF)
	ConsoleWrite(" | $$ /$$$$ /$$__  $$|_  $$_//$$$$$$| $$$$$$$$ /$$__  $$ /$$__  $$" & @CRLF)
	ConsoleWrite(" | $$|_  $$| $$$$$$$$  | $$ |______/| $$__  $$| $$  \ $$| $$  \ $$" & @CRLF)
	ConsoleWrite(" | $$  \ $$| $$_____/  | $$ /$$     | $$  | $$| $$  | $$| $$  | $$" & @CRLF)
	ConsoleWrite(" |  $$$$$$/|  $$$$$$$  |  $$$$/     | $$  | $$| $$$$$$$/| $$$$$$$/" & @CRLF)
	ConsoleWrite("  \______/  \_______/   \___/       |__/  |__/| $$____/ | $$____/ " & @CRLF)
	ConsoleWrite("                                              | $$      | $$      " & @CRLF)
	ConsoleWrite("                                              | $$      | $$      " & @CRLF)
	ConsoleWrite("                                              |__/      |__/  " & @CRLF)
	ConsoleWrite(" " & @CRLF)
	ConsoleWrite("                                                      -by Jo" & @CRLF)
	ConsoleWrite(" " & @CRLF)
	ConsoleWrite("                    Version " & $GetAppVersion & @CRLF)
	ConsoleWrite(" " & @CRLF)
	ConsoleWrite(" " & @CRLF)
	ConsoleWrite(" " & @CRLF)
	If $fast == False Then Sleep("1000")
EndFunc
Func RequiredApps()
	ConsoleWrite("     [*] Checking for required apps..." & @CRLF)
	$thisRun = epoch()
	$lastRun = IniRead($iniFile,"Log","Last Run","Never")
	$reqSeconds = $reqDays * 3600
	$reqGap = $thisRun - $lastRun
	If $reqGap >= $reqSeconds Then
		ConsoleWrite("     [*] Installing required apps..." & @CRLF)
		$apiType = "group"
		$Name = "required"
		$latest = True
		$appInfoCSV = GetAppInfo($server & ":" & $dataPort & "/group/required")
		;MsgBox(0,"",$appInfoCSV)
		InstallGroup()
	EndIf
EndFunc
Func UpdateGetApp()
	$thisRun = epoch()
	$lastRun = IniRead($iniFile,"Log","Last Run","Never")
	$reqSeconds = $reqDays * 3600
	$reqGap = $thisRun - $lastRun
	IniWrite($iniFile,"Log","Last Run",$thisRun)
	If $reqGap >= $reqSeconds Then
		ConsoleWrite("     [*] Scheduling update for " & @HOUR & ":" & @MIN + 10 & @CRLF)
		ConsoleWrite("     [*] Schedule code: " & $reqGap & @CRLF)
		run("At " & @HOUR & ":" & @MIN + 10 & " Get-App --update")
	EndIf
EndFunc
Func epoch()
	ConsoleWrite("     [*] Getting time..." & @CRLF)
    Local $rslt = DllCall("msvcrt.dll", "int:cdecl", "time", "int", 0)
    If @error = 0 Then Return $rslt[0]
    Return -1
EndFunc
Func InstallGroup()
	ConsoleWrite("     [*] Loading Group..." & @CRLF)
	Local $appInfo = StringSplit($appInfoCSV,",")
	$appInfo_ = $appInfo
	For $i = 1 To $appInfo_[0]
		$apiType = "app"
		$Name = $appInfo_[$i]
		If $Name == "" Then
			If $fast == False Then Sleep(10)
		Else
			GetAppInfo("")
			InstallFile()
			If $fast == False Then Sleep(3000)
		EndIf
	Next
EndFunc
#cs
Func testCheck()
	If GUICtrlRead($cbxLatest) == 1 Then
		GUICtrlSetState($lblVer, $GUI_DISABLE)
		GUICtrlSetState($ibxVer, $GUI_DISABLE)
		$latest = True
	Else
		GUICtrlSetState($lblVer, $GUI_ENABLE)
		GUICtrlSetState($ibxVer, $GUI_ENABLE)
		$latest = False
	EndIf
EndFunc
#ce